package tr.gov.tubitak.bilgem.esya.certviewer.viwerasn;

import com.sun.jndi.ldap.BerDecoder;
import sun.io.ByteToCharASCII;
import sun.io.ByteToCharConverter;
import sun.security.krb5.Asn1Exception;
import sun.security.util.DerEncoder;
import sun.security.util.DerValue;
import sun.security.x509.*;
import tr.gov.tubitak.bilgem.esya.certviewer.Bundle;
import tr.gov.tubitak.bilgem.esya.certviewer.EByteTools;

import java.io.IOException;
import java.security.cert.PolicyQualifierInfo;
import java.util.Enumeration;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: ramazan.girgin
 * Date: 12/6/11
 * Time: 11:02 AM
 * To change this template use File | Settings | File Templates.
 */
public class ECertificatePoliciesExtension implements ECertExtension{
    CertificatePoliciesExtension certPolicyExt;
    @Override
    public String getName() {
        return Bundle.getString("ECertificatePoliciesExtension.Name");
    }

    @Override
    public String getShortTitle() {
        StringBuilder sb = new StringBuilder();
        List<PolicyInformation> pis = null;
        try {
            pis = (List<PolicyInformation>) certPolicyExt.get(CertificatePoliciesExtension.POLICIES);
            int i = 1;
            for(PolicyInformation pi : pis){
                sb.append("["+i+"]"+ Bundle.getString("ECertificatePoliciesExtension.Name")+":\n");
                sb.append(" "+Bundle.getString("ECertificatePoliciesExtension.PolicyIdentifier")+"="+pi.getPolicyIdentifier().getIdentifier().toString()+"\n");
                int j = 1;
                for(PolicyQualifierInfo pqi : pi.getPolicyQualifiers()){
                    sb.append("  "+"["+i+","+j+"]"+ Bundle.getString("ECertificatePoliciesExtension.PolicyQualifierInfo")+":\n");
                    String policyQualifierId = pqi.getPolicyQualifierId();
                    byte[] policyQualifier = pqi.getPolicyQualifier();
                    sb.append("     "+Bundle.getString("ECertificatePoliciesExtension.PolicyQualifierId") +"="+EPolicyQualifierInfo.getPolicyIdStr(pqi.getPolicyQualifierId())+ "\n");
                    sb.append("     "+Bundle.getString("ECertificatePoliciesExtension.Qualifier")+":\n");
                    DerValue value = new DerValue(policyQualifier);
                    String asString = value.getAsString();
                    if(asString == null)
                    {
                        asString = new String(policyQualifier,"utf-16");
                        if((asString!=null) && (asString.length()>3))
                        {
                            asString = asString.substring(3,asString.length()-1);
                            asString="     "+asString;
                        }
                    }
                    sb.append("         "+asString+"\n");
                    j++;
                }
                i++;
            }

        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return sb.toString();
    }

    @Override
    public String getLongTitle() {
        return getShortTitle();
    }

    @Override
    public boolean isCritical() {
        return certPolicyExt.isCritical();
    }

    @Override
    public void encode(byte[] data) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void setAsnObject(Object asnObject) {
        if(asnObject instanceof CertificatePoliciesExtension)
        {
            certPolicyExt = (CertificatePoliciesExtension) asnObject;
        }
        //To change body of implemented methods use File | Settings | File Templates.
    }
}
